<?php
namespace app\customer\controller;
use think\Db;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Student extends Base{
    public function qiniuupload(){
        $sysres= Db::name('system')->where('id',1)->find();
        if ($sysres['file_state']==0) {
            $file=$_FILES['file'];
            $imgpath="../public/uploads/images/";
            $imgsubpath=date('Ymd',time())."/";
            if(!file_exists($imgpath)){
                mkdir($imgpath,0777);
            }
            if(!file_exists($imgpath.$imgsubpath)){
                mkdir($imgpath.$imgsubpath,0777);
            }
            chmod($imgpath.$imgsubpath,0777);
            move_uploaded_file($file['tmp_name'],$imgpath.$imgsubpath.$file['name']);
            return json(['code' => 200, 'data' =>config('tmdhttp').$_SERVER['HTTP_HOST']."/uploads/images/".$imgsubpath.$file['name'], 'msg' => '成功']);
        }elseif($sysres['file_state']==1){
            $auth = new Auth($sysres['accesskey'],$sysres['secretkey']);
            $bucket = $sysres['bucket'];
            $token = $auth->uploadToken($bucket);
            $filePath =$_FILES['file']['tmp_name'];
            $key = $_FILES['file']['name'];
            $uploadMgr = new UploadManager();
            list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
            return json(['code' => 200, 'data' =>$sysres['url'].'/'.$_FILES['file']['name'], 'msg' => '成功']);
        }
    }
    public function student_list(){
        $key=input('get.key');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $list=Db::name('student')->where('cus_id',session('customer')['id'])->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('student','like',"%{$key}%")->where('state','<',3)->order('id desc')->paginate(10);
        $lists=Db::name('student')->where('cus_id',session('customer')['id'])->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('student','like',"%{$key}%")->where('state','<',3)->select();
        $data=$list->all();
        $this->assign('key',input('get.key'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    public function student_add(){
        if(request()->isGet()){
            $cates=Db::name('tcate')->where('type',1)->where('cus_id',session('customer')['id'])->where('state',1)->order('sort desc')->select();
            $this->assign('cates',$cates);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['cus_id']=session('customer')['id'];
            $data['title']=$param['title'];
            $data['student']=$param['student'];
            $data['intro']=$param['intro'];
            $data['photo']=$param['photo'];
            $data['view']=$param['view'];
            $data['imgs']=$param['imgs'];
            $data['sort']=$param['sort'];
            $data['content']=html_entity_decode($param['content']);
            $data['state']=1;
            $data['flag']=1;
            $data['time']=time();
            $data['uptime']=time();
            $res=Db::name('student')->insert($data);
            if($res){
                return json(['code' => 200,'msg' => '成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function student_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $teachers=Db::name('student')->where('id',$param['id'])->find();
            $cates=Db::name('tcate')->where('type',1)->where('cus_id',session('customer')['id'])->where('state',1)->order('sort desc')->select();
            $teachers['imgsarr']=explode(',',$teachers['imgs']);
            $this->assign('teachers',$teachers);
            $this->assign('cates',$cates);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['student']=$param['student'];
            $data['intro']=$param['intro'];
            $data['photo']=$param['photo'];
            $data['view']=$param['view'];
            $data['imgs']=$param['imgs'];
            $data['sort']=$param['sort'];
            $data['content']=html_entity_decode($param['content']);
            $data['state']=1;
            $data['flag']=1;
            $data['uptime']=time();
            $res= Db::name('student')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('student')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('student')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function flag_off(){
        $id=input('param.id');
        $status = Db::name('student')->where(array('id'=>$id))->update(['flag'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function flag_on(){
        $id=input('param.id');
        $status = Db::name('student')->where(array('id'=>$id))->update(['flag'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function student_del(){
        $param = input('post.');
        $res= Db::name('teacher')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function student_dels(){
        $param = input('post.');
        $res= Db::name('teacher')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    // 原图上传
    public function uploadtmd(){
        $sysres= Db::name('system')->where('id',1)->find();
        $photo='';
        $imgs=[];
        if ($sysres['file_state']==0) {
            $imgpath="../public/uploads/images/";
            $imgsubpath=date('Ymd',time())."/";
            if(!file_exists($imgpath)){
                mkdir($imgpath,0777);
            }
            if(!file_exists($imgpath.$imgsubpath)){
                mkdir($imgpath.$imgsubpath,0777);
            }
            chmod($imgpath.$imgsubpath,0777);
            if(isset($_FILES['file'])){
                move_uploaded_file($_FILES['file']['tmp_name'],$imgpath.$imgsubpath.$_FILES['file']['name']);
                $photo=config('tmdhttp').$_SERVER['HTTP_HOST']."/uploads/images/".$imgsubpath.$_FILES['file']['name'];
            }
            if(isset($_FILES['imgs'])){
                for ($i=0; $i <count($_FILES['imgs']['name']) ; $i++) {
                    move_uploaded_file($_FILES['imgs']['tmp_name'][$i],$imgpath.$imgsubpath.$_FILES['imgs']['name'][$i]);
                    $imgs[$i]=config('tmdhttp').$_SERVER['HTTP_HOST']."/uploads/images/".$imgsubpath.$_FILES['imgs']['name'][$i];
                }
            }
            return json(['code' => 200, 'photo' =>$photo,'imgs' =>implode(",",$imgs), 'msg' => '成功']);
        }elseif($sysres['file_state']==1){
            $auth = new Auth($sysres['accesskey'],$sysres['secretkey']);
            $uploadMgr = new UploadManager();
            if(isset($_FILES['file'])&&!isset($_FILES['imgs'])){
                $bucket = $sysres['bucket'];
                $token = $auth->uploadToken($bucket);
                $filePath =$_FILES['file']['tmp_name'];
                $key = $_FILES['file']['name'];
                list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
                $photo=$sysres['url'].'/'.$_FILES['file']['name'];
                return json(['code' => 200, 'photo' =>$photo, 'msg' => '成功']);
            }
            if(isset($_FILES['imgs'])&&!isset($_FILES['file'])){
                for ($i=0; $i <count($_FILES['imgs']['name']) ; $i++) {
                    $bucket[$i] = $sysres['bucket'];
                    $token[$i] = $auth->uploadToken($bucket[$i]);
                    $filePath[$i] =$_FILES['imgs']['tmp_name'][$i];
                    $key[$i] = $_FILES['imgs']['name'][$i];
                    list($ret, $err) = $uploadMgr->putFile($token[$i], $key[$i], $filePath[$i]);
                    $imgs[$i]=$sysres['url'].'/'.$_FILES['imgs']['name'][$i];
                }
                return json(['code' => 200,'imgs' =>implode(",",$imgs), 'msg' => '成功']);
            }
            if(isset($_FILES['imgs'])&&isset($_FILES['file'])){
                array_push($_FILES['imgs']['error'],$_FILES['file']['error']);
                array_push($_FILES['imgs']['name'],$_FILES['file']['name']);
                array_push($_FILES['imgs']['size'],$_FILES['file']['size']);
                array_push($_FILES['imgs']['tmp_name'],$_FILES['file']['tmp_name']);
                array_push($_FILES['imgs']['type'],$_FILES['file']['type']);
                $count=count($_FILES['imgs']['name']);
                for ($i=0; $i <count($_FILES['imgs']['name']) ; $i++) {
                    $bucket[$i] = $sysres['bucket'];
                    $token[$i] = $auth->uploadToken($bucket[$i]);
                    $filePath[$i] =$_FILES['imgs']['tmp_name'][$i];
                    $key[$i] = $_FILES['imgs']['name'][$i];
                    list($ret, $err) = $uploadMgr->putFile($token[$i], $key[$i], $filePath[$i]);
                    $imgs[$i]=$sysres['url'].'/'.$_FILES['imgs']['name'][$i];
                }
                $photo=$sysres['url'].'/'.$_FILES['imgs']['name'][$count-1];
                array_splice($imgs,$count-1,1);
                return json(['code' => 200, 'photo' =>$photo,'imgs' =>implode(",",$imgs), 'msg' => '成功']);
            }
        }
    }
}
